//
//  MSSelectTimeView.m
//  SocietySale
//
//  Created by Mike on 16/11/29.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSSelectTimeView.h"

@interface MSSelectTimeView ()
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) GXButton    *doneBtn;
@property (nonatomic, strong) NSArray     *sectionArray;
@end

@implementation MSSelectTimeView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height-45) style:UITableViewStylePlain];
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height-45);
    _tableView.delegate = (id)self;
    _tableView.dataSource = (id)self;
    _tableView.scrollEnabled = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_tableView];
    
    _sectionArray = @[@"请选择开始时间",@"请选择结束时间"];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    _tableView.tableHeaderView = headerView;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 5, SCREEN_WIDTH, 25)];
    label.font = HeitiFont(14);
    label.textColor = kBlueColor;
    label.text = @"自定义";
    [self addSubview:label];
    
    self.doneBtn = [[GXButton alloc] initWithFrame:CGRectMake(0, self.height-45, SCREEN_WIDTH, 45)];
    self.doneBtn.titleLabel.font = HeitiFont(16);
    [self.doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [self.doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.doneBtn.backgroundColor = kBlueColor;
    [self.doneBtn addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.doneBtn];
}

- (void)showInView:(float)height{
//    self.height = height;
    _tableView.height = height - 45;
    self.doneBtn.top = height - 45;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellId = @"cellId";
    MSSelectTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[MSSelectTimeCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    bgView.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 200, 30)];
    label.font = HeitiFont(14);
    label.textColor = [UIColor blackColor];
    label.text = _sectionArray[section];
    [bgView addSubview:label];
    
    return bgView;
}

- (void)doneClick{
    MSSelectTimeCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    MSSelectTimeCell *cell2 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    
    NSDate *startDate = [cell getSelectDate];
    NSDate *endDate = [cell2 getSelectDate];
    if ([self.delegate respondsToSelector:@selector(selectedDate:end:)]) {
        [self.delegate selectedDate:startDate end:endDate];
    }
}

@end



@interface MSSelectTimeCell ()<UIPickerViewDelegate, UIPickerViewDataSource>
{
    NSMutableArray *_yearArray;
    NSMutableArray *_mothArray;
    NSMutableArray *_dayArray ;
}

@end


@implementation MSSelectTimeCell
@synthesize picker;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView{
    _yearArray = [NSMutableArray array];
    for (int i = 1980; i<2050; i++) {
        NSString *year = [NSString stringWithFormat:@"%d",i];
        [_yearArray addObject:year];
    }
    
    _mothArray = [NSMutableArray array];
    for (int i = 1; i<13; i++) {
        NSString *moth = [NSString stringWithFormat:@"%d",i];
        [_mothArray addObject:moth];
    }
    
    _dayArray = [NSMutableArray array];
    for (int i = 1; i<32; i++) {
        NSString *day = [NSString stringWithFormat:@"%d",i];
        [_dayArray addObject:day];
    }
    
    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(60, 0, SCREEN_WIDTH-120, 100)];
    picker.dataSource = self;
    picker.delegate = self;
    picker.showsSelectionIndicator = YES;
    [self.contentView addSubview: picker];
    
    NSArray *array1 = [self getsystemtime];
    NSString  *yearRow = array1[0];
    int year = [yearRow intValue]-1980;
    
    NSString *mothStr = array1[1];
    int moth = [mothStr intValue];
    
    NSString *dayStr = array1[2];
    int day = [dayStr intValue];
    //  设置默认选中日期
    [picker selectRow:year inComponent:0 animated:YES];
    [picker selectRow:(moth-1) inComponent:1 animated:YES];
    [picker selectRow:(day-1) inComponent:2 animated:YES];
}

#pragma mark pickerviewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
        return  _yearArray.count;
    } else if(component==1){
        return  _mothArray.count;
    }
    else if(component==2){
        NSInteger yearIndex = [picker selectedRowInComponent: 0];
        NSInteger monthIndex = [picker selectedRowInComponent: 1] +1;
        
        NSString *yearStr = _yearArray[yearIndex];
        NSString *currentStr = [NSString stringWithFormat:@"%@-%lD-1 00:00:00",yearStr,monthIndex];
        NSDate *currentSelectDate = [HDUtil getDateFromeString:currentStr];
        NSInteger monthDayNum = [self numberOfDaysInMonth:currentSelectDate];//1月中天数
        
        return monthDayNum;
    }
    return 0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 30.0f;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0 || component == 1){
        [pickerView reloadComponent:2];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0) {
        return (SCREEN_WIDTH-120) / 3;
    }
    else if (component == 1) {
        return (SCREEN_WIDTH-120) / 3;
    }
    else {
        return (SCREEN_WIDTH-120) / 3;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *myView = nil;
    
    if (component == 0) {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, (SCREEN_WIDTH-120) / 3 -5, 30)];
        myView.textAlignment = NSTextAlignmentCenter;
        myView.text = [NSString stringWithFormat:@"%@年",[_yearArray objectAtIndex:row]];
        myView.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
        myView.backgroundColor = [UIColor clearColor];
    }
    else if (component == 1) {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, (SCREEN_WIDTH-120) / 3 - 5, 30)];
        myView.textAlignment = NSTextAlignmentCenter;
        myView.text = [NSString stringWithFormat:@"%@月",[_mothArray objectAtIndex:row]];
        myView.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
        myView.backgroundColor = [UIColor clearColor];
    }
    else {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, (SCREEN_WIDTH-120) / 3 - 5, 30)];
        myView.textAlignment = NSTextAlignmentCenter;
        myView.text = [NSString stringWithFormat:@"%@日",[_dayArray objectAtIndex:row]];
        myView.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
        myView.backgroundColor = [UIColor clearColor];
    }
    
    return myView;
}

- (NSDate*)getSelectDate{
    NSInteger yearIndex = [picker selectedRowInComponent: 0];
    NSInteger monthIndex = [picker selectedRowInComponent: 1] +1;
    NSInteger dayIndex = [picker selectedRowInComponent: 2];
    
    NSString *yearStr = _yearArray[yearIndex];
    NSString *dayStr = _dayArray[dayIndex];
    
    NSString *currentStr = [NSString stringWithFormat:@"%@-%lD-%@ 00:00:00",yearStr,monthIndex,dayStr];
    NSDate *currentSelectDate = [HDUtil getDateFromeString:currentStr];
    return currentSelectDate;
}

// 获取系统时间
-(NSArray*)getsystemtime{
    NSDate *date = [NSDate date];
    NSTimeInterval  sec = [date timeIntervalSinceNow];
    NSDate *currentDate = [[NSDate alloc]initWithTimeIntervalSinceNow:sec];
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSString *na = [df stringFromDate:currentDate];
    return [na componentsSeparatedByString:@"-"];
}

-(NSInteger)numberOfDaysInMonth:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    return [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
}

@end








